$(function () {
    slsh_init();
})

function slsh_init() {
    //公司加  载
    $("#slsh-toolbar [name='_gs']").gsjgtree(1);
    //$("[name='_gs']").jgbmtree($("[name='_gs']"), 1, '', '');
    //客户加载
    $("#slsh-toolbar [name='_kh']").qyjgzcombobox();
    //工程加载
//    $("#slsh-toolbar [name='_ck']").ckcombobox();
    var options = getslshGridOption();
    $('#slsh-grid').datagrid(options);
}
function slsh_search() {
    var xsdh = $("#slsh-toolbar input[name='_xsdh']").val();
    var kh = $("#slsh-toolbar input[name='_kh']").val();
//    var ck = $("#slsh-toolbar input[name='_ck']").val();
    var xsgs = $("#slsh-toolbar input[name='_gs']").val();
    var zt = $("#slsh-toolbar input[name='zt']").val();
    var zdrqStart = $('#slsh-zdrqStart').datebox('getValue');
    var zdrqEnd = $('#slsh-zdrqEnd').datebox('getValue');
    var options = $.extend(getslshGridOption(),
        {queryParams: {xsdh: xsdh, khId: kh, xsgsId: xsgs,yck:zt, zdrqStart:zdrqStart, zdrqEnd:zdrqEnd}});
    $('#slsh-grid').datagrid(options);
}


function slsh() {
    var record = $("#slsh-grid").datagrid("getSelected");
    if (!record) {
        $.messager.alert("提示", "请选择要审核的记录!", "info");
        return;
    }
    if(record.ysh){
        $.messager.alert("提示", "已审核!", "info");
        return;
    }
    $.messager.confirm('确认', '确定审核该项', function(r){
        if (r){
            $.mask();
            var href = $.webPath() + "/kcgl/slsh/slsh_sh";
            $.post(href,{xsdId:record.id},function(data){
                $.mask.hide();
                if(data.statusCode == 200){
                    $.messager.show({title:'提示',msg:'审核成功'});
                    $('#slsh-grid').datagrid("reload");
                } else {
                    $.messager.alert("提示",data.message,'error');
                }
            },'json');
        }
    });
}
function getslshGridOption() {
    var options = {
        url: $.webPath() + "/kcgl/slsh/slshList",
        toolbar: "#slsh-toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.zt) {
                    this.zt = this.zt == 'yly' ? "已完结" : this.zt == 'yzz' ? "已终止" : this.zt == 'ytj' ? "已提交" :this.zt == 'ysp' ? "已审批": "已保存";
                    this.lx = '直达到货';
                }
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
//            if (rowData&&$("#slsh_layout input[name='xsd.id']") && $("#slsh_layout input[name='xsd.id']").val() != rowData.id) {
//                $('#slsh_layout').layout('remove', 'center');
//                $('#slsh_layout').layout('add', {
//                    region: 'center',
//                    split: true,
//                    href: $.webPath() + '/kcgl/slsh/slsh_detail?xsdId=' + rowData.id
//                });
//            }
        }
    }

    var columns = {columns: [
        [
            {field: 'xsdh', title: '销售单号',align: 'center', width: 70,formatter:slshCell},
            {field: 'lx', title: '销售类型',align: 'center', width: 60},
            {field: 'sxsj', title: '开单日期',align: 'center', width: 50},
            {field: 'kh_qc', title: '客户名称',align: 'center', width: 50},
            {field: 'xssl', title: '开单数量',align: 'right', width: 85,
                formatter:function(value){
                    var val = formatSlForHtml(value);
                    return val;
                }},
//            {field: 'thck_mc', title: '提货仓库', width: 100},
            {field: 'fyzr_z', title: '配送类型',align: 'center', width: 40},
            {field: 'dsyfje', title: '配送费用(元)',align: 'right', width: 85,
                formatter: function (value) {
                    var val = formatJeForHtml(value);
                    return val;
                }
            },
            {field: 'xsgs_qc', title: '销售公司',align: 'center', width: 70},
            {field: 'zdr', title: '制单人',align: 'center', width: 65},
            {field: 'zt', title: '状态',align: 'center', width: 40}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}